package io.laputa.offer.util;

import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTCreator;
import com.auth0.jwt.JWTVerifier;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.exceptions.JWTVerificationException;
import com.auth0.jwt.interfaces.DecodedJWT;

import java.util.Calendar;
import java.util.Locale;
import java.util.Map;

/**
 * @author Qsh
 * @since 2024/9/13
 */
public class JWTUtil {
    private static final String secret = "laputaqsh";
    private static final Algorithm algorithm = Algorithm.HMAC256(secret);
    private static final int filed = Calendar.MINUTE;
    private static final int amount = 1;

    public static String getToken(Map<String, String> payload) {
        Calendar calendar = Calendar.getInstance(Locale.CHINA);
        calendar.add(filed, amount);
        JWTCreator.Builder builder = JWT.create();
        payload.forEach(builder::withClaim);    //payload
        return builder.withExpiresAt(calendar.getTime())    //exp date
                .sign(algorithm);
    }

    public static DecodedJWT getInfo(String token) {
        return JWT.require(algorithm).build().verify(token);
    }

}
